package com.aaa.sso.service;

import com.aaa.entity.Menu;
import com.aaa.entity.User;
import com.aaa.sso.feign.MenuFeign;
import com.aaa.sso.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: aaa-his-parent
 * @description:
 * @author: 闫克起2
 * @create: 2023-12-07 11:31
 **/
@Service
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private MenuFeign menuFeign;
    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        //1.根据用户名查询用户信息
        User user=userFeign.selectUserByPhone(name);
        if(user!=null){
            //2.根据用户id查询该用户具有的权限
            List<Menu> menus = menuFeign.findMenusByUserId(user.getUserId());
            List<SimpleGrantedAuthority> collect = menus.stream().map(item -> new SimpleGrantedAuthority(item.getPercode())).collect(Collectors.toList());
            return new org.springframework.security.core.userdetails.User(user.getPhone(),user.getPassword(),collect);
        }
        return null;
    }
}





















